package components
{
	import events.ChangeStateEvent;
	
	import flash.events.MouseEvent;
	
	import model.Magazine;
	
	import mx.collections.ArrayCollection;
	
	import spark.collections.Sort;
	import spark.collections.SortField;
	import spark.components.DataGroup;
	import spark.components.DropDownList;
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.events.IndexChangeEvent;

	public class LibraryView extends SkinnableComponent
	{		
		[SkinPart(required="true")]
		public var sortDropdown: DropDownList;
				
		[SkinPart(required="true")]
		public var listFilter: DataGroup;
		
		[SkinPart(required="true")]
		public var magazinesDisplay: List;
		
		private var _magazines:ArrayCollection;
		private var magazinesChanged:Boolean = false;
		private var filterCollection:ArrayCollection = new ArrayCollection();

		public function set magazines(value:ArrayCollection):void
		{
			_magazines = value;
			magazinesChanged = true;
			invalidateProperties();
		}
		
		public function get magazines():ArrayCollection
		{
			return _magazines;
		}				

		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			switch (instance)
			{
				case listFilter:
					listFilter.addEventListener(MouseEvent.CLICK, filterEventHandler);
					break;
				case sortDropdown:
					updateSorting(sortDropdown.selectedItem);
					sortDropdown.addEventListener(IndexChangeEvent.CHANGE, sortEventHandler);
					break;
				case magazinesDisplay:
					magazinesDisplay.addEventListener(IndexChangeEvent.CHANGE, selectMagazine);
					break;
			}
		}

		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (magazinesChanged)
			{
				magazinesChanged = false;
				magazinesDisplay.dataProvider = _magazines;

				filterCollection = new ArrayCollection();
				filterCollection.addItem("All");
				for each (var m:Magazine in _magazines)
				{
					if (!filterCollection.contains(m.publisherName))
						filterCollection.addItem(m.publisherName);
				}
				listFilter.dataProvider = filterCollection;
			}
		}
		
		private function updateFilter(filterString:String):void
		{
			var tmpfilterFunction:Function = function(item:Object):Boolean
			{
				return (filterString == "All") || filterString == Magazine(item).publisherName;
			}
			
			if (_magazines != null)
			{
				_magazines.filterFunction = tmpfilterFunction;
				_magazines.refresh();
			}
		}
		
		private function updateSorting(sortString:String):void
		{
			var tmpSort:Sort = new Sort();

			if (sortString == "Title")
			{
				tmpSort.fields = [new SortField("name", true)];
			}			
			else if (sortString == "Date Published")
			{
				tmpSort.fields = [new SortField("datePublished", true)];
			}
			
			if (_magazines != null)
			{
				_magazines.sort = tmpSort;
				_magazines.refresh();
			}
		}
		
		protected function filterEventHandler(e:MouseEvent):void 
		{
			updateFilter(e.target.text.substr(2));
			updateSorting(sortDropdown.selectedItem);
		}

		protected function sortEventHandler(e:IndexChangeEvent):void 
		{
			updateSorting(e.target.selectedItem);
		}
		
		protected function selectMagazine(e:IndexChangeEvent):void
		{
			dispatchEvent(new ChangeStateEvent(ChangeStateEvent.CHANGE_STATE, e.currentTarget.selectedItem));
		}
	}
}